package uk.ac.cam.cl.android.jwdb;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SwipeIt implements Action, OnClickListener {
	Level level;
	Button button;
	private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private int direction;
    private String directionText;

	@Override
	public void display(Button button,Level level) {
		// TODO Auto-generated method stub
		direction = Math.min((int) (Math.random() * 4.0), 3);
		initialiseDirectionText();
		button.setText("Swipe It " + directionText);
		this.level = level;
		this.button = button;
	}
	
	private void initialiseDirectionText() {
		if (direction == 0) {
			directionText = "Left";
		} else if (direction == 1) {
			directionText = "Right";
		} else if (direction == 2) {
			directionText = "Up";
		} else if (direction == 3) {
			directionText = "Down";
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean check() {
		
		// Gesture detection
        gestureDetector = new GestureDetector(new SwipeGestureDetector(direction, this));
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
        button.setOnTouchListener(gestureListener);
        
		return true;
	}

	@Override
	public void done(){
		level.nextAction();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
}
